<?php

/**
 *  52zll.top
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

require DISCUZ_ROOT.'./source/plugin/dzapp_zhibo/class/zhiboController.php';
$ops = array('list', 'post', 'delete', 'live', 'post_graphic', 'graphic', 'comment', 'advert', 'post_advert', 'manager', 'end');
$op = !in_array($_GET['op'], $ops) ? 'list' : $_GET['op'];

$perpage = 20;
$page = max(1, intval($_GET['page']));
$start = ($page - 1) * $perpage;
if($start < 0) $start = 0;

if($op == 'list') {

	if(!submitcheck('listsubmit')) {

		$_GET['end'] = $_GET['end'] == '-1' ? '' : $_GET['end'];
		$param = array(
			array('name', $_GET['name'], 'like', '%', '%'),
			array('end', $_GET['end']),
		);
	
		$count = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->count_by_search($param);
		if($count) {
			$query = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->fetch_all_by_search($param, $start, $perpage);
			foreach($query as $value) {
				$activity_id[] = $value['activity_id'];
				$value['qrcode'] = urlencode($_G['siteurl'].'plugin.php?id=dzapp_zhibo&mod=view&identify='.$value['activity_id']);
				$list[] = $value;
			}
			unset($_GET['page']);
			$mpurl = 'plugin.php?'.page_url($_GET);
			$multipage = multi($count, $perpage, $page, $mpurl);
		}
		$live = C::t('#dzapp_zhibo#dzapp_zhibo_live')->fetch_all_by_search(array(array('activity_id', $activity_id)));
		foreach($live as $value){	
			$lives[$value['activity_id']] = $value;
		}
		
	} else {

		if(is_array($_GET['delete'])) {
			C::t('#dzapp_zhibo#dzapp_zhibo_activity')->delete($_GET['delete']);
		}
		
		showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=activity');
	}

} elseif($op == 'end'){
	
	$zhibo = new zhiboController;
	$activity = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->fetch($_GET['activity_id']);
	$param =  array(
		'activityId' => $activity['activityid'],
	);

	$zhibo->zhibo('lecloud.cloudlive.activity.stop', $param);
	C::t('#dzapp_zhibo#dzapp_zhibo_activity')->update($_GET['activity_id'], array('end' => 1));
	showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=activity');
	
} elseif($op == 'live') {
	
	$param = array(
		array('activity_id', $_GET['activity_id'])
	);
	$count = C::t('#dzapp_zhibo#dzapp_zhibo_live')->count_by_search($param);
	if($count) {
		$query = C::t('#dzapp_zhibo#dzapp_zhibo_live')->fetch_all_by_search($param, $start, $perpage);
		foreach($query as $value) {
			$list[] = $value;
		}
		unset($_GET['page']);
		$mpurl = 'plugin.php?'.page_url($_GET);
		$multipage = multi($count, $perpage, $page, $mpurl);
	}

} elseif($op == 'graphic') {
	
	if(!submitcheck('listsubmit')) {
		
		$param = array(
			array('activity_id', $_GET['activity_id'])
		);
		$count = C::t('#dzapp_zhibo#dzapp_zhibo_graphic')->count_by_search($param);
		if($count) {
			$query = C::t('#dzapp_zhibo#dzapp_zhibo_graphic')->fetch_all_by_search($param, $start, $perpage);
			foreach($query as $value) {
				$value['dateline'] = gmdate("Y-m-d H:i:s", $value['dateline']);
				$list[] = $value;
			}
			unset($_GET['page']);
			$mpurl = 'plugin.php?'.page_url($_GET);
			$multipage = multi($count, $perpage, $page, $mpurl);
		}
		
	} else {

		if(is_array($_GET['delete'])) {
			C::t('#dzapp_zhibo#dzapp_zhibo_graphic')->delete($_GET['delete']);
		}
		
		showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=activity&op=graphic&activity_id='.$_GET['activity_id']);
	}

} elseif($op == 'post_graphic'){
	
	if($_GET['graphic_id']){
		
		$graphic = C::t('#dzapp_zhibo#dzapp_zhibo_graphic')->fetch($_GET['graphic_id']);
		if(empty($graphic)) {
			showmessage('dzapp_zhibo:required_validate');
		}
		$_GET['pic'] = $graphic['pic'];
		$graphic['pic'] = $graphic['pic'] ? dzapp_pic_get($graphic['pic'], 'portal', 0, $_G['setting']['ftp']['on']) : 'static/image/common/nophoto.gif';
		$_GET['activity_id'] = $graphic['activity_id'];
	}
	
	if(submitcheck('graphicsubmit')) {
		
		if($pic = dzapp_pic_upload($_FILES['pic'], 'portal', 320, 240)) $_GET['pic'] = $pic['pic'];
		if($pic && $graphic['pic']) dzapp_pic_delete($graphic['pic'], 'portal', 1);
		
		$setarr = array(
			'name'        => $_GET['name'],
			'activity_id' => $_GET['activity_id'],
			'pic'         => $_GET['pic'],
			'video_url'   => $_GET['video_url'],
			'url'         => $_GET['url'],
			'describe'    => $_GET['describe'],
		);
		if($_GET['graphic_id']){
		
			C::t('#dzapp_zhibo#dzapp_zhibo_graphic')->update($_GET['graphic_id'], $setarr);
			
		}else{
			
			$setarr['dateline'] = $_G['timestamp'];
			$setarr['ip'] = $_G['ip'];
			C::t('#dzapp_zhibo#dzapp_zhibo_graphic')->insert($setarr);
		}
		
		showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=activity&op=graphic&activity_id='.$_GET['activity_id']);
	}
	
} elseif($op == 'comment'){
	
	if(!submitcheck('listsubmit')) {
		
		$param = array(
			array('id', $_GET['activity_id'])
		);
		$count = C::t('#dzapp_zhibo#dzapp_zhibo_comment')->count_by_search($param);
		if($count) {
			$query = C::t('#dzapp_zhibo#dzapp_zhibo_comment')->fetch_all_by_search($param, $start, $perpage);
			foreach($query as $value) {
				$value['dateline'] = gmdate("Y-m-d H:i:s", $value['dateline']);
				$list[] = $value;
			}
			unset($_GET['page']);
			$mpurl = 'plugin.php?'.page_url($_GET);
			$multipage = multi($count, $perpage, $page, $mpurl);
		}
		
	} else {

		if(is_array($_GET['delete'])) {
			C::t('#dzapp_zhibo#dzapp_zhibo_comment')->delete($_GET['delete']);
		}
		
		showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=activity&op=comment&activity_id='.$_GET['activity_id']);
	}
	
} elseif($op == 'manager'){
	
	if(!submitcheck('managersubmit')) {
		
		$activity = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->fetch($_GET['activity_id']);
		
	} else {

		$setarr = array(
			'manager' => $_GET['manager'],
		);
		
		C::t('#dzapp_zhibo#dzapp_zhibo_activity')->update($_GET['activity_id'], $setarr);
		
		showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=activity');
	}
	
} elseif($op == 'post') {

	if($_GET['activity_id']){
		
		$activity = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->fetch($_GET['activity_id']);
		if(empty($activity)) {
			showmessage('dzapp_zhibo:required_validate');
		}
		
		$_GET['cover_img_url'] = $activity['cover_img_url'];
		$_GET['advert'] = $activity['advert'];
		$activity['cover_img_url'] = $activity['cover_img_url'] ? dzapp_pic_get($activity['cover_img_url'], 'portal', 0, $_G['setting']['ftp']['on']) : 'static/image/common/nophoto.gif';
		$activity['advert'] = $activity['advert'] ? dzapp_pic_get($activity['advert'], 'portal', 0, $_G['setting']['ftp']['on']) : 'static/image/common/nophoto.gif';
	}
	
	if(submitcheck('activitysubmit')) {
		
		if(!$_GET['name'] || !$_GET['start_time'] || !$_GET['end_time']) {
			showmessage('dzapp_zhibo:required_validate');
		}
		
		if($cover_img_url = dzapp_pic_upload($_FILES['cover_img_url'], 'portal', 320, 240)) $_GET['cover_img_url'] = $cover_img_url['pic'];
		if($cover_img_url && $activity['cover_img_url']) dzapp_pic_delete($activity['cover_img_url'], 'portal', 1);
		if($advert = dzapp_pic_upload($_FILES['advert'], 'portal', 320, 240)) $_GET['advert'] = $advert['pic'];
		if($advert && $activity['advert']) dzapp_pic_delete($activity['advert'], 'portal', 1);
		
		$data = array(
			'name'              => $_GET['name'],
			'start_time'        => $_GET['start_time'],
			'end_time'          => $_GET['end_time'],
			'cover_img_url'     => $_GET['cover_img_url'],
			'live_num'          => 1,
			'code_rate'         => $_GET['code_rate'],
			'need_record'       => 1,
			'need_time_shift'   => $_GET['need_time_shift'],
			'need_full_view'    => $_GET['need_full_view'],
			'activity_category' => $_GET['activity_category'],
			'play_mode'         => $_GET['play_mode'],
			'scroll_advert'     => $_GET['scroll_advert'],
			'scroll_advert_url' => $_GET['scroll_advert_url'],
			'advert'            => $_GET['advert'],
			'advert_url'        => $_GET['advert_url'],
			'graphic_status'    => $_GET['graphic_status'],
			'tab_title'         => $_GET['tab_title'],
			'tab_content'       => $_GET['tab_content'],
		);
		
		if($activity['activity_id']){
			C::t('#dzapp_zhibo#dzapp_zhibo_activity')->update($activity['activity_id'], $data);
		}else{
			
			$data['dateline'] = $_G['timestamp'];
			$data['ip'] = $_G['ip'];
			
			$activity_id = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->insert($data, true);
			$zhibo = new zhiboController;
			$method_name = "letv.cloudlive.activity.create";
			$param = array(
				'activityName'     => diconv($_GET['name'], CHARSET, 'UTF-8'),
				'startTime'        => date('YmdHis', strtotime($_GET['start_time'])),
				'endTime'          => date('YmdHis', strtotime($_GET['end_time'])),
				'coverImgUrl'      => $_GET['cover_img_url'],
				'description'      => diconv($_GET['description'], CHARSET, 'UTF-8'),
				'liveNum'          => 1,
				'codeRateTypes'    => $_GET['code_rate'],
				'needRecord'       => 1,
				'needTimeShift'    => $_GET['need_time_shift'],
				'needFullView'     => $_GET['need_full_view'],
				'activityCategory' => $_GET['activity_category'],
				'playMode'         => $_GET['play_mode'],
			);
			
			$rs = $zhibo->zhibo($method_name, $param);
			
			C::t('#dzapp_zhibo#dzapp_zhibo_activity')->update($activity_id, array('activityid' => $rs['activityId'])); 
			$param = array(
				'activityId'                => $rs['activityId'],
				'neededPushAuth'            => '1',
				'pushUrlValidTime'          => '80000',
				'needIpWhiteList'           => '0',
				'needPlayerDomainWhiteList' => '0',
			);
			$zhibo->zhibo('letv.cloudlive.activity.sercurity.config', $param);
			
			$param =  array(
				'activityId' => $rs['activityId'],
			);

			$rs = $zhibo->zhibo('letv.cloudlive.activity.getPushUrl', $param, 'get');
			$zhibo->zhibo_ver = '3.1';
		
			$res = $zhibo->zhibo('lecloud.cloudlive.vrs.activity.streaminfo.search', $param, 'get');
			
			foreach($rs['lives'] as $value){
				$param = array(
					'machine'     => $value['machine'],
					'push_url'    => str_replace('rtmp://w.gslb.lecloud.com/live/', '', $value['pushUrl']),
					'status'      => $value['status'],
					'stream_id'   => $value['streamId'],
					'activity_id' => $activity_id,
					'start_time'  => date('YmdHis', strtotime($_GET['start_time'])),
					'end_time'    => date('YmdHis', strtotime($_GET['end_time'])),
				);
				foreach($res['lives'] as $val){
					if($value['machine'] == $val['machine']){
						//$param['taskid'] = $zhibo->zhibo('lecloud.cloudlive.rec.createRecTask', array('liveId' => $val['liveId'], 'startTime' => date('YmdHis', strtotime($_GET['start_time'])), 'endTime' => date('YmdHis', strtotime($_GET['end_time']))));
						$param['liveid'] = $val['liveId'];
					}
				}
				C::t('#dzapp_zhibo#dzapp_zhibo_live')->insert($param);
			}
			
			
		}
		showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=activity');
		
	}
}

include_once template("dzapp_zhibo:zhibocp_activity");

?>